﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BfLib.Web
{
    public class UrlAbsolutizer
    {
        private Uri _absUri, _relUri, _resultUri;
        private string _absUrl = string.Empty, _relUrl = string.Empty;

        /*
            // Create an absolute Uri from a string.
            Uri absoluteUri = new Uri("http://www.contoso.com/");

            // Create a relative Uri from a string.  allowRelative = true to allow for 
            // creating a relative Uri.
            Uri relativeUri = new Uri("/catalog/shownew.htm?date=today", UriKind.Relative);

            // Check whether the new Uri is absolute or relative.
            if (!relativeUri.IsAbsoluteUri)
                Console.WriteLine("{0} is a relative Uri.", relativeUri);

            // Create a new Uri from an absolute Uri and a relative Uri.
            Uri combinedUri = new Uri(absoluteUri, relativeUri);
            Console.WriteLine(combinedUri.AbsoluteUri);
        */
        public string ResultUrl
        {
            get
            {
                if (_relUri == null)
                {
                    return null;
                }
                else if (_relUri.IsAbsoluteUri)
                {
                    return _relUri.AbsoluteUri;
                }
                else
                {
                    _resultUri = new Uri(_absUri, _relUri);
                    return _resultUri.AbsoluteUri;
                }
            }
        }

        public UrlAbsolutizer()
        {

        }

        public string GetAbsoluteUrl(string AbsoluteUrl, string RelativeUrl)
        {
            if (!Uri.IsWellFormedUriString(AbsoluteUrl, UriKind.Absolute))
                throw new ArgumentException(String.Format("Malformed absolute url: {0}", AbsoluteUrl));

            //if (!Uri.IsWellFormedUriString(RelativeUrl, UriKind.RelativeOrAbsolute))
            //    throw new ArgumentException(String.Format("Malformed relative/absolute url: {0}", RelativeUrl));

            _relUri = new Uri(RelativeUrl, UriKind.RelativeOrAbsolute);
            _absUri = new Uri(AbsoluteUrl, UriKind.Absolute);


            if (_relUri == null)
            {
                return null;
            }
            else if (_relUri.IsAbsoluteUri)
            {
                return _relUri.AbsoluteUri;
            }
            else
            {
                _resultUri = new Uri(_absUri, _relUri);
                return _resultUri.AbsoluteUri;
            }
        }

        public UrlAbsolutizer(Uri AbsoluteUri)
        {
            _absUri = AbsoluteUri;

            if (!_absUri.IsAbsoluteUri)
                throw new ArgumentException(String.Format("Malformed absolute url: {0}", _absUri.ToString() ));
        }

        public UrlAbsolutizer(string AbsoluteUrl)
        {
            if (!Uri.IsWellFormedUriString(AbsoluteUrl, UriKind.Absolute))
                throw new ArgumentException(String.Format("Malformed absolute url: {0}", AbsoluteUrl));

            _absUri = new Uri(AbsoluteUrl, UriKind.Absolute);
        }

        public UrlAbsolutizer(Uri AbsoluteUri, Uri RelativeUri)
        {
            if (!AbsoluteUri.IsAbsoluteUri)
                throw new ArgumentException(String.Format("Malformed absolute url: {0}", AbsoluteUri.ToString()));

            _absUri = AbsoluteUri;
            _relUri = RelativeUri;
        }

        public UrlAbsolutizer(Uri AbsoluteUri, string RelativeUrl)
        {
            if (!AbsoluteUri.IsAbsoluteUri)
                throw new ArgumentException(String.Format("Malformed absolute url: {0}", AbsoluteUri.ToString()));

            //if (!Uri.IsWellFormedUriString(RelativeUrl, UriKind.RelativeOrAbsolute))
            //    throw new ArgumentException(String.Format("Malformed relative/absolute url: {0}", RelativeUrl));

            _absUri = AbsoluteUri;
            _relUri = new Uri(RelativeUrl, UriKind.RelativeOrAbsolute);
        }

        public UrlAbsolutizer(string AbsoluteUrl, string RelativeUrl)
        {
            if (!Uri.IsWellFormedUriString(AbsoluteUrl, UriKind.Absolute))
                throw new ArgumentException(String.Format("Malformed absolute url: {0}", AbsoluteUrl));

            //if (!Uri.IsWellFormedUriString(RelativeUrl, UriKind.RelativeOrAbsolute))
            //    throw new ArgumentException(String.Format("Malformed relative/absolute url: {0}", RelativeUrl));

            _relUri = new Uri(RelativeUrl, UriKind.RelativeOrAbsolute);
            _absUri = new Uri(AbsoluteUrl, UriKind.Absolute);
        }

        public UrlAbsolutizer(string AbsoluteUrl, Uri RelativeUri)
        {
            if (!Uri.IsWellFormedUriString(AbsoluteUrl, UriKind.Absolute))
                throw new ArgumentException(String.Format("Malformed absolute url: {0}", AbsoluteUrl));

            //if (!Uri.IsWellFormedUriString(RelativeUrl, UriKind.RelativeOrAbsolute))
            //    throw new ArgumentException(String.Format("Malformed relative/absolute url: {0}", RelativeUrl));

            _absUri = new Uri(AbsoluteUrl, UriKind.Absolute);
            _relUri = RelativeUri;
        }



        public void SetRelativeUri(Uri RelativeUri)
        {
            _relUri = RelativeUri;
        }

        public void SetRelativeUrl(string RelativeUrl)
        {
            //if (!Uri.IsWellFormedUriString(RelativeUrl, UriKind.RelativeOrAbsolute))
            //    throw new ArgumentException(String.Format("Malformed relative/absolute url: {0}", RelativeUrl));

            _relUri = new Uri(RelativeUrl, UriKind.RelativeOrAbsolute);
        }
    }
}
